package org.csu.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.domain.*;
import org.csu.demo.persistence.AccountMapper;
import org.csu.demo.persistence.BannerDataMapper;
import org.csu.demo.persistence.ProfileMapper;
import org.csu.demo.persistence.SignMapper;
import org.csu.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private ProfileMapper profileMapper;

    @Autowired
    private BannerDataMapper bannerDataMapper;

    public CommonResponse<User> updateUser(User user,String username) {
        //认为是第三方登录
        if(isUsernameExist(username).getMessage().equals("Not Exist")){
            //新增其字段，模板为demo
            User user1 = getUserByUsername("demo").getData();
            Sign sign = user1.getSign();
            Account account = user1.getAccount();
            Profile profile = user1.getProfile();
            sign.setUsername(username);
            account.setUsername(username);
            profile.setUsername(username);
            user1.setSign(sign);
            user1.setAccount(account);
            user1.setProfile(profile);
            register(user1);
        }
        //更新非空值到数据库
        Account account = user.getAccount();
        Sign sign = user.getSign();
        Profile profile = user.getProfile();
        boolean flag = true;
        if (account != null) {
            account.setUsername(username);
            flag = accountMapper.updateById(account)>0;
        }
        if (sign != null) {
            if(!sign.getUsername().equals(username)){
                sign.setUsername(username);
                flag = signMapper.updateById(sign)>0;
            }

        }
        if (profile != null && profile.notAllNull()) {
            UpdateWrapper<Profile> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userid", username);
            if(profile.getLanguagePreference()!=null){
                updateWrapper.set("langpref",profile.getLanguagePreference());
            }
            if(profile.getFavoriteCategory()!=null){
                updateWrapper.set("favcategory",profile.getFavoriteCategory());
            }
            if(profile.isBannerOption()){
                updateWrapper.set("banneropt",true);
            }
            if(profile.isMyListOption()){
                updateWrapper.set("mylistopt",true);
            }
            profile.setUsername(username);
            flag = profileMapper.update(updateWrapper)>0;
        }
        if (flag) {
            return CommonResponse.createForSuccess();
        }else{
            return CommonResponse.createForError();
        }
    }

    public CommonResponse<User> login(String username, String password){
        QueryWrapper<Sign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        Sign sign = signMapper.selectOne(queryWrapper);
        Profile profile = profileMapper.selectById(username);
        if(sign != null && profile != null){
            sign.setPassword(null);
            Account account = accountMapper.selectById(username);
            BannerData bannerData = bannerDataMapper.selectById(profile.getFavoriteCategory());
            User user = new User();
            sign.setPassword(null);
            user.setAccount(account);
            user.setProfile(profile);
            user.setSign(sign);
            user.setBannerData(bannerData);
            return CommonResponse.createForSuccess("登录成功",user);
        }else {
            return CommonResponse.createForError("密码错误");
        }
    }

    //用于判断用户名是否重复，只用查看一个表即可
    public CommonResponse<User> isUsernameExist(String username){
        Sign sign = signMapper.selectById(username);
        /*Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);*/
        User user = new User();
        if(sign != null /*&& account != null && profile != null*/){
            /*user.setAccount(account);
            user.setProfile(profile);*/
            sign.setPassword(null);
            user.setSign(sign);
            return CommonResponse.createForSuccessMessage("Exist");
        }else{
            return CommonResponse.createForSuccessMessage("Not Exist");
        }
    }


    //根据用户名获取用户信息
    public CommonResponse<User> getUserByUsername(String username){
        Sign sign = signMapper.selectById(username);
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        User user = new User();
        if(sign != null && account != null && profile != null){
            user.setAccount(account);
            user.setProfile(profile);
            if(!username.equals("demo")){
                sign.setPassword(null);
            }
            user.setSign(sign);
            if(user.getProfile().isBannerOption()){
                user.setBannerData(bannerDataMapper.selectById(profile.getFavoriteCategory()));
            }
            return CommonResponse.createForSuccess(user);
        }else{
            return CommonResponse.createForError();
        }
    }

    //用户注册，可以在controller判断数据是否合法，或者在前端判断
    public CommonResponse<User> register(User user){
        if(isUsernameExist(user.getSign().getUsername()).getMessage().equals("Not Exist")){
            if(accountMapper.insert(user.getAccount())>0 && signMapper.insert(user.getSign())>0 && profileMapper.insert(user.getProfile())>0){
                return CommonResponse.createForSuccessMessage("注册成功");
            }else{
                return CommonResponse.createForError("注册失败，用户名重复");
            }
        }else{
            return CommonResponse.createForError("注册失败，用户名重复");
        }

    }
}
